from concurrent.futures import ThreadPoolExecutor
import threading
from Tools import Tools
import os,datetime,time
import pandas as pd
path ="E:\\tdx_export\\20220206_hdf_final\\"
def func(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]

liststr=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
count_list =[]
N=100
liststr=os.listdir(path)
def get_list(title="xxx" ):
    # __dir =os.listdir(path)
    Tools.logger().success("分段函数\n")
    __dir =liststr
    # print(__dir)
    i=0
    for x in func(__dir,N):
        # Tools.logger().success(str(i)+","+str(x)+","+str(len(x)))
        count_list.append(str(i))
        i+=1
    # print("count_list",count_list)
    return count_list
    # print(count_list)
    # for __file in __dir:
    #     print(os.path.join(path,__file))
        # df = pd.read_csv(os.path.join(path,__file))
        # print(__file)

def exec(name):
    # Tools.logger().info("线程执行函数"+str(name))
    # print("Thread name :",name,"\n")
    __dir = liststr
    xxx = func(__dir, N)
    # for xxx in func(__dir, N):
    fd_data = list(xxx)[int(name)]
    # print(fd_data)
    # print(len(fd_data))
    for __file in fd_data:
        df= read_csv(path,__file)
        df.to_csv("data/"+__file+".txt")
    return len(fd_data),threading.current_thread().name + '  ' + str(name)

def read_csv(path,file):
    # print(os.path.join(path,file))
    df = pd.read_csv(os.path.join(path,file))
    return df
import sys
def run_thread(title=""):
    with ThreadPoolExecutor(max_workers=1000) as pool:
        # print(type(get_list()))
        thread_lsit =get_list()
        Tools.logger().success ("执行分段列表函数" + str(thread_lsit))
        inputstr = input("程序会分成"+str(len(thread_lsit))+"个线程执行")
        if inputstr !="1" :
            sys.exit(2)
        # 使用线程执行map计算
        # 后面元组有3个元素，因此程序启动3条线程来执行action函数

        Tools.logger().warning("===========================线程开始执行========================\n")
        results = pool.map(exec,thread_lsit)
        print('='*150)
        Tools.logger().critical(results)
        print("="*150)
        for r in results:
            print("reslts",r)
if __name__ == '__main__':
    os.system("cls")
    starttime = datetime.datetime.now()
    Tools.logger().info("program is running....")
    Tools.running("xxx",runfunc=run_thread)
    Tools.logger().critical(threading.active_count())
    Tools.logger().success("Program is ending...")
    # print(threading.active_count(), len(threading.enumerate()), threading.enumerate())
    # # 查看线程数量
    # thread_num = len(threading.enumerate())
    # print("主线程：线程数量是%d" % thread_num)
    # # 输出所有线程名字
    # print(str(threading.enumerate()))
    # print("主线程：主线程结束")
    endtime = datetime.datetime.now()
    Tools.logger().success('程序运行时间:' + str(endtime - starttime))